<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon.ico">
  <link rel="mask-icon" href="/blogs/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blogs/css/main.css">


<link rel="stylesheet" href="/blogs/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    hostname: new URL('https://tretea.gitee.io').hostname,
    root: '/blogs/',
    scheme: 'Gemini',
    version: '7.6.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    comments: {"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}
  };
</script>

  <meta name="description" content="python是一门面向对象的语言,关于python的类,实例以及他们的一些特性">
<meta property="og:type" content="article">
<meta property="og:title" content="python面向对象">
<meta property="og:url" content="https://tretea.gitee.io/2020/02/07/python%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/index.html">
<meta property="og:site_name" content="小叶子的个人博客">
<meta property="og:description" content="python是一门面向对象的语言,关于python的类,实例以及他们的一些特性">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-02-07T02:48:04.000Z">
<meta property="article:modified_time" content="2020-03-28T11:01:57.295Z">
<meta property="article:author" content="小叶子">
<meta property="article:tag" content="python基础">
<meta property="article:tag" content="面向对象">
<meta property="article:tag" content="动态语言">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://tretea.gitee.io/2020/02/07/python%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>python面向对象 | 小叶子的个人博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/blogs/atom.xml" title="小叶子的个人博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
    <a href="https://github.com/tretea" target="_blank" rel="noopener" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/blogs/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">小叶子的个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/blogs/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blogs/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blogs/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blogs/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blogs/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://tretea.gitee.io/2020/02/07/python%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blogs/images/%E5%B0%8F%E5%8F%B6%E5%AD%90.jpg">
      <meta itemprop="name" content="小叶子">
      <meta itemprop="description" content="也许我会走的很慢，但我不会停下来">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小叶子的个人博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          python面向对象
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-02-07 10:48:04" itemprop="dateCreated datePublished" datetime="2020-02-07T10:48:04+08:00">2020-02-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-03-28 19:01:57" itemprop="dateModified" datetime="2020-03-28T19:01:57+08:00">2020-03-28</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blogs/categories/python/" itemprop="url" rel="index">
                    <span itemprop="name">python</span>
                  </a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/blogs/2020/02/07/python%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/#comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/blogs/2020/02/07/python%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.3k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>7 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>python是一门面向对象的语言,关于python的类,实例以及他们的一些特性</p>
<a id="more"></a>
<h1 id="一、面向对象的发展"><a href="#一、面向对象的发展" class="headerlink" title="一、面向对象的发展"></a>一、面向对象的发展</h1><p>面向对象是一种编程思路,与之对应的还有面向过程的编程思路<br>面向对象是将所有的问题都抽象成对象,通过对象的属性与方法来解决问题<br>而面向过程是将问题分步骤解决<br><strong>举例:一个人要从家里到学校</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">面向过程的解决思路:</span><br><span class="line">stu1=&#123;name:<span class="string">'小叶子'</span>,age:<span class="number">20</span>,sex:<span class="string">'man'</span>&#125;</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">leave_bed</span><span class="params">(people)</span>:</span></span><br><span class="line">    print(<span class="string">f'<span class="subst">&#123;people.name&#125;</span>正在起床...'</span>)</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">eat</span><span class="params">(people)</span>:</span></span><br><span class="line">    print(<span class="string">f'<span class="subst">&#123;people.name&#125;</span>正在吃饭...'</span>)</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">go_school</span><span class="params">(people)</span>:</span></span><br><span class="line">    print(<span class="string">f'<span class="subst">&#123;people.name&#125;</span>正在去学校...'</span>)</span><br><span class="line">leave_bed(stu1)</span><br><span class="line">eat(stu1)</span><br><span class="line">go_school(stu1)</span><br><span class="line"></span><br><span class="line">结果输出:</span><br><span class="line">    <span class="string">'小叶子正在起床...'</span></span><br><span class="line">    <span class="string">'小叶子正在吃饭...'</span></span><br><span class="line">    <span class="string">'小叶子正在去学校...'</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">面向对象的解决思路:</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Go_school</span><span class="params">(object)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,name,age,sex)</span>:</span></span><br><span class="line">        self.name=name</span><br><span class="line">        self.age=age</span><br><span class="line">        self.sex=sex</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">leave_bed</span><span class="params">(self)</span>:</span></span><br><span class="line">        print(<span class="string">f'<span class="subst">&#123;self.name&#125;</span>正在起床...'</span>)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">eat</span><span class="params">(self)</span>:</span></span><br><span class="line">        print(<span class="string">f'<span class="subst">&#123;self.name&#125;</span>正在吃饭...'</span>)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">go_school</span><span class="params">(self)</span>:</span></span><br><span class="line">        print(<span class="string">f'<span class="subst">&#123;self.name&#125;</span>正在去学校...'</span>)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">execute</span><span class="params">(self)</span>:</span></span><br><span class="line">        self.leave_bed()</span><br><span class="line">        self.eat()</span><br><span class="line">        self.go_school()</span><br><span class="line"></span><br><span class="line">stu1=Go_school(<span class="string">'小叶子'</span>,<span class="number">20</span>,<span class="string">'man'</span>)</span><br><span class="line">stu1.execute()</span><br><span class="line"></span><br><span class="line">结果输出:</span><br><span class="line">    <span class="string">'小叶子正在起床...'</span></span><br><span class="line">    <span class="string">'小叶子正在吃饭...'</span></span><br><span class="line">    <span class="string">'小叶子正在去学校...'</span></span><br></pre></td></tr></table></figure>

<h1 id="二、类的概念"><a href="#二、类的概念" class="headerlink" title="二、类的概念"></a>二、类的概念</h1><p>类是根据大自然的运行规律抽象而来,使人更容易理解编程语言<br>类就是类别,分辨两个东西是否属于同一个类别,就是根据这些东西的特征,比如<strong>动物的类别</strong>和<strong>植物的类别</strong>如何区分,动物<strong>有五官,有四肢,有五脏六腑这些属性</strong>,也有<strong>吃饭,跑步,睡觉这些方法</strong>,动物类别的五官四肢可以叫做动物类的<strong>属性</strong>,属性可以用字典类型来描述,{‘eyes’:’big’,’legs’:’long’}就是一个人类<strong>属性</strong>,行为可以用<strong>函数</strong>来描述<br>类就是对象的模板,类有两个基本特征:<code>1.属性 2.方法</code>通过<strong>属性</strong>和<strong>方法</strong>就可以确定一个类</p>
<h3 id="类的定义"><a href="#类的定义" class="headerlink" title="类的定义"></a>类的定义</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span><span class="params">(object)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,name,age,sex)</span>:</span></span><br><span class="line">        self.name=name</span><br><span class="line">        self.age=age</span><br><span class="line">        self.sex=sex</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">print_name</span><span class="params">(self)</span>:</span></span><br><span class="line">        print(self.__name)</span><br></pre></td></tr></table></figure>
<p>以上代码就是一个类的创建,包函 <strong>关键字</strong>:<code>class</code> <strong>类名</strong><code>Student</code>(首字母大写表明是一个类名) <strong>继承类</strong>:<code>object</code> <strong>初始化函数</strong><code>__ init __</code><br>类是一个模板,<strong>self</strong>表示模糊的实例化对象,<strong>self. name,self.age,self.sex</strong>是对象属性,name,age,sex是形参等待实例化后赋具体的值</p>
<h3 id="理解"><a href="#理解" class="headerlink" title="理解"></a>理解</h3><p>类就是确定一类对象的模板,以上例子中的类为该类的对象创造了name,age,sex属性与print_name方法</p>
<h1 id="三、实例对象的概念"><a href="#三、实例对象的概念" class="headerlink" title="三、实例对象的概念"></a>三、实例对象的概念</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">stu1=Student(<span class="string">'小叶子'</span>,<span class="number">20</span>,<span class="string">'man'</span>)</span><br><span class="line">stu1就是实例化的一个对象,具有name,age,sex三个具体属性值,以及print_name的行为方法</span><br></pre></td></tr></table></figure>
<p>实例化的对象,就是以类作为模板,为实例属性赋具体的值,获得的对象</p>
<h1 id="四、类方法-实例方法-静态方法-类属性-实例属性"><a href="#四、类方法-实例方法-静态方法-类属性-实例属性" class="headerlink" title="四、类方法,实例方法,静态方法;类属性,实例属性"></a>四、类方法,实例方法,静态方法;类属性,实例属性</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">student</span><span class="params">(object)</span>:</span></span><br><span class="line">    stu_num=<span class="number">0</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,name,age,sex)</span>:</span></span><br><span class="line">        self.name=name</span><br><span class="line">        self.age=age</span><br><span class="line">        self.sex=sex</span><br><span class="line">        student.stu_num+=<span class="number">1</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">print_name</span><span class="params">(self)</span>:</span></span><br><span class="line">        print(self.__name)</span><br><span class="line"><span class="meta">    @classmethod</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">print_stu_num</span><span class="params">(cls)</span>:</span></span><br><span class="line">        print(cls.stu_num,<span class="string">'这是执行类方法后输出的类属性'</span>)</span><br><span class="line"><span class="meta">    @staticmethod</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">msg</span><span class="params">()</span>:</span></span><br><span class="line">        print(<span class="string">'这是一个静态方法'</span>)</span><br></pre></td></tr></table></figure>
<p>stu_num表示Student的<strong>类属性</strong>,类属性是属于整个类的,不属于单个对象,所以每个对象都可以通过类调用这个类属性,无论实例化多少个对象,类属性不会重置,可以用来记录实例化了多少个对象<br><strong>@classmethod装饰器</strong>使得方法转变为类方法,默认参数cls表示类,可以通过cls调用类方法与类属性,但无法调用实例方法与实例属性<br><strong>@staticmethod装饰器</strong>使得方法转变为静态方法,无法调用类方法类属性和实例方法实例属性  </p>
<h1 id="五、类的三要素"><a href="#五、类的三要素" class="headerlink" title="五、类的三要素"></a>五、类的三要素</h1><p>类的三要素为<strong>封装</strong>,<strong>继承</strong>,<strong>多态</strong>  </p>
<h3 id="1-封装性"><a href="#1-封装性" class="headerlink" title="1.封装性"></a>1.封装性</h3><p>将属性与方法封装到一个类中,外部无法看到类中的代码,只能调用类方法</p>
<h3 id="2-继承"><a href="#2-继承" class="headerlink" title="2.继承"></a>2.继承</h3><p>继承后的类被称作<strong>子类</strong>,被继承的类叫做<strong>父类,超类,基类</strong>,子类会直接获得父类的属性与方法,所以叫做继承,子类也可以修改父类的属性与方法<br>object是初始类,如果没有可继承的类,要继承object</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span><span class="params">(object)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">eat</span><span class="params">(self)</span>:</span></span><br><span class="line">        print(<span class="string">'我正在吃饭...'</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span><span class="params">(Animal)</span>:</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line">dog1=Dog()</span><br><span class="line">dog1.eat()</span><br><span class="line"></span><br><span class="line">结果输出 <span class="string">'我正在吃饭'</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Cat</span><span class="params">(Animal)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">eat</span><span class="params">(self)</span>:</span></span><br><span class="line">        print(<span class="string">'猫正在吃饭'</span>)</span><br><span class="line">cat1=Cat()</span><br><span class="line">cat1.eat()</span><br><span class="line"></span><br><span class="line">结果输出 <span class="string">'猫正在吃饭'</span></span><br></pre></td></tr></table></figure>

<h3 id="3-多态"><a href="#3-多态" class="headerlink" title="3.多态"></a>3.多态</h3><p>不同的子类,由于不同的特性,要修改父类的属性方法或者不修改,导致子类的不同,叫做多态,俗称,<strong>龙生九子,各有不同</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span><span class="params">(object)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">eat</span><span class="params">(self)</span>:</span></span><br><span class="line">        print(<span class="string">'我正在吃饭...'</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span><span class="params">(Animal)</span>:</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line">dog1=Dog()</span><br><span class="line">dog1.eat()</span><br><span class="line"></span><br><span class="line">结果输出 <span class="string">'我正在吃饭'</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Cat</span><span class="params">(Animal)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">eat</span><span class="params">(self)</span>:</span></span><br><span class="line">        print(<span class="string">'猫正在吃饭'</span>)</span><br><span class="line">cat1=Cat()</span><br><span class="line">cat1.eat()</span><br><span class="line"></span><br><span class="line">结果输出 <span class="string">'猫正在吃饭'</span></span><br></pre></td></tr></table></figure>

<h1 id="六、类的魔法方法"><a href="#六、类的魔法方法" class="headerlink" title="六、类的魔法方法"></a>六、类的魔法方法</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># __init__:初始化实例</span></span><br><span class="line"><span class="comment"># __str__:print(实例)输出内容</span></span><br><span class="line"><span class="comment"># __repr__:实例 输出内容,用于调试</span></span><br><span class="line"><span class="comment"># __iter__:生成一个可迭代的类</span></span><br><span class="line"><span class="comment"># __next__:进行下一次取值</span></span><br><span class="line"><span class="comment"># __getitem__:生成一个可切片和取索引的类</span></span><br><span class="line"><span class="comment"># __call__():可以使得对象像函数一样调用</span></span><br><span class="line"><span class="comment"># callable():可以判断是否是可调用的函数类型</span></span><br></pre></td></tr></table></figure>

<h1 id="七、类的权限"><a href="#七、类的权限" class="headerlink" title="七、类的权限"></a>七、类的权限</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 双下划线开头,双下划线结尾的变量为特殊变量,如__name__,__init__,该类变量有特殊意义,命名变量不能以这样的形式命名</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 单下划线开头,表明不希望对象外部直接查看和修改,但是仍然可以查看和修改,不严格;可以通过对象内部的方法进行查看和修改</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span><span class="params">(object)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,name)</span>:</span></span><br><span class="line">        self._name=name</span><br><span class="line">stu1=Student(<span class="string">'小叶子'</span>)</span><br><span class="line">print(stu1._name)</span><br><span class="line">stu1._name=<span class="string">'叶子'</span></span><br><span class="line">print(stu1._name)</span><br><span class="line">结果输出 </span><br><span class="line">    小叶子</span><br><span class="line">    叶子</span><br><span class="line"></span><br><span class="line"><span class="comment"># 双下划线开头,更加严格的不能在对象外部查看和修改;可以通过对象内部的方法进行查看和修改  </span></span><br><span class="line"><span class="comment"># 变量名设置双下划线开头之后,实际上的变量名就被更改了,如果用自己定义的变量名进行修改,并不能修改属性,只能新增属性</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span><span class="params">(object)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,name)</span>:</span></span><br><span class="line">        self.__name=name</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">print_name</span><span class="params">(self)</span>:</span></span><br><span class="line">        print(self.__name)</span><br><span class="line">stu1=Student(<span class="string">'小叶子'</span>)</span><br><span class="line">stu1.print_name</span><br><span class="line">stu1.__name=<span class="string">'叶子'</span></span><br><span class="line">stu1.print_name</span><br><span class="line">结果输出 </span><br><span class="line">    小叶子</span><br><span class="line">    小叶子</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 普通变量,既可以通过对象内部的方法进行查看和修改,又可以在外部直接修改</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># self代表实例化后的对象,可以通过点语法访问实例属性和实例方法,</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># cls/类名代表类,可以通过点语法访问类属性和类方法,</span></span><br></pre></td></tr></table></figure>
<h1 id="八、type"><a href="#八、type" class="headerlink" title="八、type()"></a>八、type()</h1><h3 id="type-定义类"><a href="#type-定义类" class="headerlink" title="type()定义类"></a>type()定义类</h3><p>由于python是动态语言,动态语言创建类的过程:代码执行class关键字时,会调用type(),type不仅能查看对象的类,更能创建一个新类<br>格式:类名=type(类名,继承的对象(元组形式存放,),方法(字典形式存放))</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn</span><span class="params">(self)</span>:</span></span><br><span class="line">    print(<span class="string">'这是一个学生类'</span>)</span><br><span class="line">Students=type(<span class="string">'Students'</span>,(object,),&#123;<span class="string">'print_name'</span>:fn&#125;)</span><br><span class="line">stu=Students()</span><br><span class="line">stu.print_name()</span><br><span class="line"></span><br><span class="line">结果输出 <span class="string">'这是一个学生类'</span></span><br></pre></td></tr></table></figure>

<p><code>(1)</code>不是元组类型,是整型<br><code>(1,)</code>是元组类型</p>
<h1 id="九、类的更底层"><a href="#九、类的更底层" class="headerlink" title="九、类的更底层"></a>九、类的更底层</h1><h3 id="元类"><a href="#元类" class="headerlink" title="元类"></a>元类</h3><p>在类创建过程中,类的样式受元类的影响,元类会定义一个<strong>new</strong>()方法,该方法确定类名,继承对象合集,类方法合集,是类的更底层</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 元类:类的模板,继承自type,是类创建的前提,可以动态修改类</span></span><br><span class="line"><span class="comment"># __new__()有四个参数,1.类对象 2.类名 3.继承类合集 4.方法合集</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ListMetaclass</span><span class="params">(type)</span>:</span></span><br><span class="line">   <span class="function"><span class="keyword">def</span> <span class="title">__new__</span><span class="params">(cls, name, bases, attrs)</span>:</span></span><br><span class="line">        attrs[<span class="string">'add'</span>] = <span class="keyword">lambda</span> self, value: self.append(value)</span><br><span class="line">        <span class="keyword">return</span> type.__new__(cls, name, bases, attrs)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyList</span><span class="params">(list,metaclass=ListMetaclass)</span>:</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line">L = MyList()</span><br><span class="line">L.add(<span class="number">1</span>)</span><br><span class="line">print(L)</span><br><span class="line"></span><br><span class="line">结果输出 [<span class="number">1</span>]</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

      <div>
  
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
  
</div>
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blogs/tags/python%E5%9F%BA%E7%A1%80/" rel="tag"># python基础</a>
              <a href="/blogs/tags/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" rel="tag"># 面向对象</a>
              <a href="/blogs/tags/%E5%8A%A8%E6%80%81%E8%AF%AD%E8%A8%80/" rel="tag"># 动态语言</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blogs/2020/02/05/python%E8%A3%85%E9%A5%B0%E5%99%A8/" rel="prev" title="python装饰器">
      <i class="fa fa-chevron-left"></i> python装饰器
    </a></div>
      <div class="post-nav-item">
    <a href="/blogs/2020/02/07/hexo%E5%8D%9A%E5%AE%A2next%E4%B8%BB%E9%A2%98%E6%B7%BB%E5%8A%A0valine%E8%AF%84%E8%AE%BA%E7%B3%BB%E7%BB%9F%E5%92%8C%E5%9B%9E%E5%A4%8D%E9%82%AE%E7%AE%B1%E6%8F%90%E9%86%92/" rel="next" title="hexo博客next主题添加valine评论系统和回复邮箱提醒">
      hexo博客next主题添加valine评论系统和回复邮箱提醒 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#一、面向对象的发展"><span class="nav-number">1.</span> <span class="nav-text">一、面向对象的发展</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#二、类的概念"><span class="nav-number">2.</span> <span class="nav-text">二、类的概念</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#类的定义"><span class="nav-number">2.0.1.</span> <span class="nav-text">类的定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#理解"><span class="nav-number">2.0.2.</span> <span class="nav-text">理解</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#三、实例对象的概念"><span class="nav-number">3.</span> <span class="nav-text">三、实例对象的概念</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#四、类方法-实例方法-静态方法-类属性-实例属性"><span class="nav-number">4.</span> <span class="nav-text">四、类方法,实例方法,静态方法;类属性,实例属性</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#五、类的三要素"><span class="nav-number">5.</span> <span class="nav-text">五、类的三要素</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-封装性"><span class="nav-number">5.0.1.</span> <span class="nav-text">1.封装性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-继承"><span class="nav-number">5.0.2.</span> <span class="nav-text">2.继承</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-多态"><span class="nav-number">5.0.3.</span> <span class="nav-text">3.多态</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#六、类的魔法方法"><span class="nav-number">6.</span> <span class="nav-text">六、类的魔法方法</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#七、类的权限"><span class="nav-number">7.</span> <span class="nav-text">七、类的权限</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#八、type"><span class="nav-number">8.</span> <span class="nav-text">八、type()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#type-定义类"><span class="nav-number">8.0.1.</span> <span class="nav-text">type()定义类</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#九、类的更底层"><span class="nav-number">9.</span> <span class="nav-text">九、类的更底层</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#元类"><span class="nav-number">9.0.1.</span> <span class="nav-text">元类</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="小叶子"
      src="/blogs/images/%E5%B0%8F%E5%8F%B6%E5%AD%90.jpg">
  <p class="site-author-name" itemprop="name">小叶子</p>
  <div class="site-description" itemprop="description">也许我会走的很慢，但我不会停下来</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blogs/archives/">
        
          <span class="site-state-item-count">57</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blogs/categories/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blogs/tags/">
          
        <span class="site-state-item-count">48</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tretea" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tretea" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/tretea" title="gitee → https:&#x2F;&#x2F;gitee.com&#x2F;tretea" rel="noopener" target="_blank"><i class="fa fa-fw fa-gitee"></i>gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:2433469529@qq.com" title="E-Mail → mailto:2433469529@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://space.bilibili.com/88409744" title="bilibili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;88409744" rel="noopener" target="_blank"><i class="fa fa-fw fa-bilibili"></i>bilibili</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">
  
  &copy; 2019 – 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-孙智超"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">孙智超</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">84k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">2:20</span>
</div>

<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blogs/lib/anime.min.js"></script>
  <script src="/blogs/lib/velocity/velocity.min.js"></script>
  <script src="/blogs/lib/velocity/velocity.ui.min.js"></script>

<script src="/blogs/js/utils.js"></script>

<script src="/blogs/js/motion.js"></script>


<script src="/blogs/js/schemes/pisces.js"></script>


<script src="/blogs/js/next-boot.js"></script>




  




  
<script src="/blogs/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el: '#valine-comments',
      verify: false,
      notify: true,
      appId: 'joFkPTxLK8qvkQ7bk3l11d9w-gzGzoHsz',
      appKey: 'FoRjyOtjoXMgYfOHTlzmL1iQ',
      placeholder: "请输入您的评论",
      avatar: '小叶子',
      meta: guest,
      pageSize: '10' || 10,
      visitor: false,
      lang: '' || 'zh-cn',
      path: location.pathname,
      recordIP: false,
      serverURLs: ''
    });
  }, window.Valine);
});
</script>

<script src="/blogs/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"model":{"jsonPath":"/blogs/live2dw/assets/wanko.model.json"},"display":{"position":"right","width":300,"height":300},"mobile":{"show":false},"log":false});</script></body>
</html>
